package com.zahb.qadx.ui.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.zahb.qadx.R;
import com.zahb.qadx.base.BaseFragmentExt;
import com.zahb.qadx.model.CateNavigationModel;
import com.zahb.qadx.ui.adapter.NewsFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by： huwei.
 * Data： 2020-10-22.
 * Time： 15:24.
 * Desc：全部
 */
public class NewsAllFragment extends BaseFragmentExt {
    @BindView(R.id.tab_news)
    TabLayout tabNews;
    @BindView(R.id.vp_new)
    ViewPager2 vpNew;
    private NewsFragmentAdapter pagerAdapter;
    private List<CateNavigationModel.NavigationData> new_tab_list = new ArrayList<>();
    private List<Fragment> new_vp_list;
    private List<String> listTitle;
    private int id;

    @Override
    protected int getLayout() {
        return R.layout.fragment_attention;
    }

    @Override
    protected void initViews(View rootView) {
        mUnbinder = ButterKnife.bind(this, rootView);
        new_tab_list = new ArrayList<>();
        new_vp_list = new ArrayList<>();
        listTitle = new ArrayList<>();
        id = mArguments.getInt("id", -1);
        ArrayList<CateNavigationModel.NavigationData> list = mArguments.getParcelableArrayList("child");
        if (list != null && !list.isEmpty()) {
            getNavigationListData(list);
        } else {
            NewsItemFragment fragment = new NewsItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            bundle.putString("name", "全部");
            fragment.setArguments(bundle);
            new_vp_list.add(0, fragment);
            listTitle.add(0, "全部");
            pagerAdapter = new NewsFragmentAdapter(this, new_vp_list);
            vpNew.setAdapter(pagerAdapter);
            TabLayoutMediator mediator = new TabLayoutMediator(tabNews, vpNew,
                    (tab, position) -> tab.setText(listTitle.get(position)));
            mediator.attach();
            tabNews.setVisibility(View.GONE);
        }
    }

    private void getNavigationListData(List<CateNavigationModel.NavigationData> response) {
        new_tab_list.clear();
        new_tab_list.addAll(response);
        for (int i = 0; i < new_tab_list.size(); i++) {
            CateNavigationModel.NavigationData item = new_tab_list.get(i);
            NewsItemFragment fragment = new NewsItemFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("id", item.getCateId());
            bundle.putString("name", item.getCateName());
            fragment.setArguments(bundle);
            new_vp_list.add(fragment);
            listTitle.add(item.getCateName());
        }

        //全部
        NewsItemFragment fragment = new NewsItemFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        bundle.putString("name", "全部");
        fragment.setArguments(bundle);
        new_vp_list.add(0, fragment);
        listTitle.add(0, "全部");
        pagerAdapter = new NewsFragmentAdapter(this, new_vp_list);
        vpNew.setAdapter(pagerAdapter);
        vpNew.setOffscreenPageLimit(new_vp_list.size() - 1);
        TabLayoutMediator mediator = new TabLayoutMediator(tabNews, vpNew,
                (tab, position) -> tab.setText(listTitle.get(position)));
        mediator.attach();
    }
}
